package com.platform.toutiao.util.http;

import java.io.IOException;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;

import com.platform.toutiao.error.TouTiaoErrorException;

import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;

public class ApacheSimpleGetRequestExecutor extends SimpleGetRequestExecutor<CloseableHttpClient, HttpHost> {
	public ApacheSimpleGetRequestExecutor(RequestHttp requestHttp) {
		super(requestHttp);
	}

	@Override
	public String execute(String uri, String queryParam) throws TouTiaoErrorException, IOException {
		if (queryParam != null) {
			if (uri.indexOf('?') == -1) {
				uri += '?';
			}
			uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
		}
		HttpGet httpGet = new HttpGet(uri);
		if (requestHttp.getRequestHttpProxy() != null) {
			RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
			httpGet.setConfig(config);
		}

		try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet)) {
			String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
			return handleResponse(responseContent);
		} finally {
			httpGet.releaseConnection();
		}
	}

}
